package com.oolatina;

import com.google.android.youtube.player.YouTubePlayerView;

import android.content.Context;
import android.view.Display;
import android.view.WindowManager;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.util.DisplayMetrics;

/**
 * Created by jlaurent on 01/04/14.
 */
public class SCYoutubeView
{
    WebView webContent;
    FrameLayout.LayoutParams contentSize;
    String urlYoutube;
    int ScreenWidth;
    int ScreenHeight;
    int SizeWidth;
    int SizeHeight;
    int _width;
    int _height;
    int _x;
    int _y;
    DisplayMetrics metrics;
    
    public void init(FrameLayout layout, Context thisContext)
    {
        WindowManager wm = (WindowManager) thisContext.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        ScreenWidth = display.getWidth();
        ScreenHeight = display.getHeight();
        
        metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);

        metrics.widthPixels /= metrics.density;


        urlYoutube = "http://www.youtube.com/embed/";
        contentSize = new FrameLayout.LayoutParams(300, 200);
        contentSize.setMargins(0,0,0,0);

        webContent = new WebView(thisContext);
        WebSettings webSettings = webContent.getSettings();
        //webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginState(PluginState.ON);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setAllowFileAccess(true);
        //webSettings.setUseWideViewPort(false);
        //webSettings.setLoadWithOverviewMode(false);
        webContent.getSettings().setLoadWithOverviewMode(false);
        webContent.getSettings().setUseWideViewPort(false);
        webContent.getSettings().setJavaScriptEnabled(true);
        webContent.setWebChromeClient(new WebChromeClient());
        webContent.setBackgroundColor(0x00000000);
        webContent.setWebViewClient(new WebViewClient());
        webContent.setInitialScale(100);
        webContent.setPadding(0, 0, 0, 0);
        webContent.setLayoutParams(contentSize);
        layout.addView(webContent);
    }

    public void setSize(int width,int height)
    {
    	_width = width;
    	_height = height;
    	SizeWidth = width;
    	SizeHeight = height;
        webContent.getLayoutParams().width = width;
        webContent.getLayoutParams().height = height;
    }
    
    public void SetPosition(int x, int y)
    {
    	_x = x;
		_y = y;
		
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				_width,_height);
        layoutParams.setMargins(_x,_y,0,0);
        webContent.setLayoutParams(layoutParams);   	
    }

    public void setSizePourcent(int width,int height)
    {
    	float radioWidth = (float)ScreenWidth/100;
    	float radioHeight = (float)ScreenHeight/100;
    	_width = (int)(radioWidth*width);
    	_height = (int)(radioHeight*height);
		
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				_width,_height);
        layoutParams.setMargins(_x,_y,0,0);
        webContent.setLayoutParams(layoutParams);
    }

    public void setId(String idyoutube)
    {
        String html;
        html = "<html><body style=\"margin:0;\"><iframe type=\"text/html5\" style=\"width:"
            + _width
            + "px;height:"
            + _height
            + "px;\" src=\""
            + urlYoutube + idyoutube
            + "\" frameborder=\"0\"></iframe></body></html>";
        Log.d("HTML : ",html);
        webContent.loadData(html,"text/html", "utf-8");
    }
    
    
}
